from common.public import *
from orm.model import Dept, User, Team
from view.baseview import BaseView


class DeptView(BaseView):
    def __init__(self):
        pass

    def getAllDept(self):
        """取得所有部门"""
        depts = Dept.select().order_by(Dept.name.asc())
        alldept = []
        for dept in depts:
            if dept.type == COMMON_DEPT:
                alldept.append({"id":dept.id,"name":dept.name})
        return super().resultSet(200, "部门列表取得成功！",  alldept)

    def getGroups(self):
        """取得所有组"""
        depts = Dept.select().where(Dept.type == COMMON_GROUP).order_by(Dept.name.asc())
        alldept = []
        for dept in depts:
            alldept.append({"id":dept.id,"name":dept.name})
        return super().resultSet(200, "分组列表取得成功！",  alldept)

    def getCombination(self):
        """取得组织结构"""
        data = []
        # 根部门  总经办
        deptroot = Dept.select().where(Dept.up_id == 0).first()
        rootnode = {"id":deptroot.id,"type":COMMON_DEPT,"label":deptroot.name,"children":[]}
        # 2级部门 市场部、会计部、人事部
        sub2depts = Dept.select().where(Dept.up_id == deptroot.id).order_by(Dept.name)
        for dept in sub2depts:
            sub2node = {"id":dept.id,"type":COMMON_DEPT,"label":dept.name,"children":[]}
            # 3级部门 市场部-无锡，市场部-绍兴，财税部
            sub3depts = Dept.select().where(Dept.up_id == dept.id).order_by(Dept.name)
            for dept3 in sub3depts:
                sub3node = {"id":dept3.id,"type":COMMON_DEPT,"label":dept3.name,"children":[]}
                # 4级部门
                sub4depts = Dept.select().where(Dept.up_id == dept3.id).order_by(Dept.name)
                for dept4 in sub4depts:
                    sub4node = {"id":dept4.id, "type":COMMON_DEPT, "label":dept4.name, "children":[]}
                    sub3node["children"].append(sub4node)
                # 4级团队
                sub4teams = User.select().where((User.dept_id == dept3.id) & (User.isleader == COMMON_TRUE) & (User.isactive == COMMON_TRUE))
                for leader in sub4teams:
                    if leader.ismanager == COMMON_FALSE:
                        sub4node = {"id":leader.id,"type":COMMON_TEAM,"label":leader.username + "-团队","children":[]}
    #                 users = Team.select().where(Team.leader_id == leader.id)
    #                 for user in users:
    #                     usernode = {"label":user.member.username}
    #                     sub3node["children"].append(usernode)
                        sub3node["children"].append(sub4node)
                sub2node["children"].append(sub3node)
            rootnode["children"].append(sub2node)
        allnode = {"id":0,"label":"全部","children":[]}
        allnode["children"].append(rootnode)
        data.append(allnode)
        print(allnode)
        return super().resultSet(200, "组织结构取得成功！",  data)

if __name__ == '__main__':
    view = DeptView()
    data = view.getCombination()
    print(data)
